package linkedList.双指针.剑指;

import linkedList.BaseLinkedListTest;
import po.ListNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 剑指offer删除链表的节点18.java
 * @Description TODO https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
 * @createTime 2021年03月21日 10:55:00
 */
public class 剑指offer删除链表的节点18 extends BaseLinkedListTest {

    public static ListNode deleteNode(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        if (head.val == val) {
            return head.next;
        }
        ListNode temp = head, tempNext = head.next;
        while (tempNext != null && tempNext.val != val) {
            temp = temp.next;
            tempNext = tempNext.next;
        }
        if (tempNext == null) {
            return head;
        } else {
            if (tempNext.next == null) {
                temp.next=null;
            } else {
                temp.next = tempNext.next;
            }
        }

        return head;
    }

    public static void main(String[] args) {
        deleteNode(createLinkedList(-3, 5, -99), -99);
    }
}
